<template>
  <d2-container class="page">
    <div class="form-box">
      <div class="form-item">
        <div class="form-label">事件发生时间：</div>
        <div class="form-input">
          <el-date-picker
            v-model="date"
            type="daterange"
            value-format="yyyyMMdd"
            unlink-panels
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            @change="changeDateHandle"
          >
          </el-date-picker>
        </div>
      </div>
      <div class="form-item">
        <div class="form-label">事件发生地点：</div>
        <div class="form-input">
          <el-select
            v-model="params.eventCountry"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(item, index) in locationOptions"
              :key="index"
              :label="item.value"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="form-item">
        <div class="form-label">处置状态：</div>
        <div class="form-input">
          <el-select
            v-model="params.handleStatus"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(item, index) in statusOptions"
              :key="index"
              :label="item.value"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="form-item">
        <div class="form-input">
          <el-input
            v-model="params.keyword"
            placeholder="请输入关键字"
            clearable
          >
            <el-button
              type="primary"
              class="search-btn"
              slot="append"
              @click="onSubmit"
            >搜索</el-button>
          </el-input>
        </div>
      </div>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        border
        height="600"
        style="width: 100%"
      >
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.key"
          :label="item.title"
        >
          <template slot-scope="scope">
            <span v-if="item.key === 'id'">{{scope.row[item.key]}}</span>
            <span v-if="item.key === 'eventTitle'">{{scope.row[item.key]}}</span>
            <span v-if="item.key === 'reportTime'">{{scope.row[item.key]}}</span>
            <span v-if="item.key === 'eventCountry'">{{scope.row[item.key]}}</span>
            <span v-if="item.key === 'handleStatus'">{{scope.row[item.key] === '0' ? '正在办理' : scope.row[item.key] === '1' ? '办理完成' : ''}}</span>
            <div
              class="button-action"
              v-if="item.key === 'action'"
            >
              <el-button
                type="text"
                size="small"
                @click="openInfoDialog(scope.row)"
              >详情</el-button>
              <el-button
                type="text"
                size="small"
                @click="openReplyDialog(scope.row)"
              >回复</el-button>
              <el-button
                type="text"
                size="small"
                @click="onDelete({ index: scope.$index, row: scope.row })"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="params.page"
        :page-size="params.limit"
        layout="prev, pager, next, jumper, sizes"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 回复 -->
    <el-dialog
      title="回复"
      :visible.sync="replyVisible"
      :modal-append-to-body="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="500px"
      :before-close="handleCloseReply"
    >
      <div class="reply-content">
        <div class="form-content">
          <el-input
            v-model="replyParams.replyContent"
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 6 }"
            placeholder="请输入回复内容"
          >
          </el-input>
        </div>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handleSubmitReply"
        >提交</el-button>
        <el-button
          type="primary"
          @click="handleResetReply"
        >重置</el-button>
      </span>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      title="详情"
      :visible.sync="infoVisible"
      :modal-append-to-body="false"
      width="800px"
      :before-close="handleCloseInfo"
    >
      <div class="info-content">
        <reply-info
          :value="infos"
          :imgList="imgList"
        ></reply-info>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handleCloseInfo"
        >确定</el-button>
      </span>
    </el-dialog>
  </d2-container>
</template>

<script>
import { mapActions } from 'vuex'
import ReplyInfo from './info'
import { request } from '@/api/service'

const tableColumns = [
  { title: 'ID', key: 'id' },
  { title: '标题', key: 'eventTitle' },
  { title: '上报时间', key: 'reportTime' },
  { title: '发生地', key: 'eventCountry' },
  { title: '处置状态', key: 'handleStatus' },
  { title: '操作', key: 'action' }
]
export default {
  name: 'group',
  components: {
    ReplyInfo
  },
  data() {
    return {
      tableColumns,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      tableData: [],
      date: [],
      // 事件地点option
      locationOptions: [],
      // 处置状态option
      statusOptions: [
        { code: 0, value: '正在办理' },
        { code: 1, value: '办理完成' }
      ],
      params: {
        startDate: '',
        endDate: '',
        handleStatus: '',
        keyword: '',
        eventCountry: '',
        page: 1,
        limit: 10
      },
      total: 0,
      replyVisible: false,
      replyParams: {
        replyContent: '',
        eventId: ''
      },
      infoVisible: false,
      infos: null,
      imgList: []
    }
  },
  created() {
    const end = new Date()
    const start = new Date()
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 29)
    this.date = [start, end]
    this.params.startDate = this.$dateFormat('yyyyMMdd', start)
    this.params.endDate = this.$dateFormat('yyyyMMdd', end)
    this.getDictionary('country')
  },
  mounted() {
    this.$nextTick(() => {
      this.queryList()
    })
  },
  methods: {
    ...mapActions('d2admin/pm.dictionary', [
      'dictLoad'
    ]),
    ...mapActions('d2admin/pm.index', [
      'getEvent',
      'deleteEvent',
      'postReply'
    ]),
    // 获取字典
    getDictionary(type) {
      this.dictLoad({ type }).then(res => {
        if (type === 'country') {
          this.locationOptions = res.country
        }
      })
    },
    // 回显字典
    showDicValue(id, dict) {
      if (!dict) {
        return
      }
      return dict.map(item => {
        if (id === item.code) {
          return item.value
        }
      })
    },
    // 切换日期
    changeDateHandle(date) {
      if (date) {
        this.params.startDate = date[0]
        this.params.endDate = date[1]
      } else {
        // this.params.startDate = this.$dateFormat('yyyyMMdd', new Date())
        // this.params.endDate = this.$dateFormat('yyyyMMdd', new Date())
        this.params.startDate = ''
        this.params.endDate = ''
      }
    },
    // 查询列表
    queryList() {
      this.loading = true
      this.getEvent(this.params).then(res => {
        this.loading = false
        res.list.forEach(item => {
          item.eventCountry = item.eventCountry && this.showDicValue(item.eventCountry, this.locationOptions).filter(item2 => {
            return item2
          })[0]
        })
        this.tableData = res.list
        this.total = res.totalCount
        // eslint-disable-next-line handle-callback-err
      }).catch(err => {
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.params.limit = val
      this.params.page = 1
      this.queryList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.queryList()
    },
    // 点击查询按钮
    onSubmit() {
      this.params.page = 1
      this.params.keyword = this.params.keyword.trim()
      this.queryList()
    },
    // 打开回复
    openReplyDialog(row) {
      this.replyVisible = true
      this.replyParams.eventId = row.id
    },
    handleCloseReply() {
      this.replyVisible = false
    },
    handleSubmitReply() {
      if (this.replyParams.replyContent === '') {
        this.$message.error('请输入回复内容')
        return
      }
      this.handleReply()
    },
    handleResetReply() {
      this.replyParams.replyContent = ''
    },
    handleReply() {
      this.postReply(this.replyParams).then(res => {
        if (res.code === 0) {
          this.replyVisible = false
        }
      })
    },
    // 删除
    onDelete({ index, row }) {
      this.$confirm('您确定删除吗？', '提示')
        .then(res => {
          this.deleteEvent({
            id: row.id
          }).then(res => {
            this.queryList()
          })
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 详情
    openInfoDialog(row) {
      this.handleGetInfo(row.id)
    },
    handleGetInfo(id) {
      const opts = {
        url: '/pm/event/info/' + id,
        method: 'get'
      }
      request(opts).then(res => {
        if (res.code === 0) {
          this.imgList = []
          res.data.photos.forEach(item => {
            this.imgList.push(item.url)
          })
          this.infos = res.data
          this.infoVisible = true
        }
      })
    },
    handleCloseInfo() {
      this.infoVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  .form-box {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
    .form-item {
      display: flex;
      margin-right: 20px;
      margin-bottom: 20px;
      .form-label {
        width: 120px;
        text-align: right;
        color: #606266;
        line-height: 40px;
      }
    }
  }
  .table {
    margin-top: 20px;
  }
  .search-btn {
    color: #fff;
    background-color: #409eff;
    border-color: #409eff;
    border-radius: 0;
  }
  .el-pagination {
    margin-top: 20px;
    text-align: center;
  }
  .reply-content {
    display: flex;
    .form-label {
      width: 120px;
      text-align: right;
      color: #606266;
      font-size: 16px;
    }
    .form-content {
      flex: 1;
    }
  }
}
</style>
